import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { User as UserEntity } from '../../modules/users/entities/user.entity';

/**
 * 用户装饰器
 * 用于从请求中获取当前登录用户信息
 * 
 * @example
 * @Get('profile')
 * getProfile(@User() user: UserEntity) {
 *   return user;
 * }
 * 
 * @example
 * // 获取用户的特定字段
 * @Get('profile')
 * getProfile(@User('id') userId: number) {
 *   return { userId };
 * }
 */
export const User = createParamDecorator(
  (data: keyof UserEntity | undefined, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();
    const user = request.user;
    
    return data ? user?.[data] : user;
  },
);